package com.marketingexpert.controller.publicmgnt;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.marketingexpert.config.Placeholder;
import com.marketingexpert.service.snsmgnt.Status;

public class AnalysisWeiboContentUtil {

	public static List<Status> generateStatus(JSONArray  publicweiboArray , String weiboType,String cat, String q) {
		if(weiboType.equalsIgnoreCase("sinaWeibo")) {
			return AnalysisWeiboContentUtil.generateSinaStatus(publicweiboArray,cat, q);
		} else {
			return AnalysisWeiboContentUtil.generateTencentStatus(publicweiboArray,cat, q);
		}
	}
	
	private static List<Status> generateSinaStatus(JSONArray publicweiboArray,String cat, String q) {
			List<Status> statuses = new ArrayList<Status>();
			JSONObject statusJson = null;
			for(int i=0; i<publicweiboArray.size(); i++) {
					statusJson = publicweiboArray.getJSONObject(i);				
					boolean isRetweet = (statusJson.get("retweeted_status") != null);					
					Status status = new Status();
					if(q == null) {
							Pattern p = Pattern.compile(cat);
							Matcher m = p.matcher(statusJson.getString("text"));
							if(!m.find()) {
								continue;
							}
							
							if(isRetweet) {
								statusJson = statusJson.getJSONObject("retweeted_status");
							}
							
					} else {
						if(isRetweet) {
							statusJson = statusJson.getJSONObject("retweeted_status");
						}
						Pattern p = Pattern.compile(q);
						Matcher m = p.matcher(statusJson.getString("text"));
						if(!m.find()) {
							continue;
						}
					}
					status.setText(statusJson.getString("text"));
					String source = statusJson.getString("source");

					String sourceName = source.substring(source.indexOf(">") + 1, source.length() - 4);
					status.setSourceName(sourceName);
					status.setSource(source);	
					status.setDate(new Date(statusJson.getString("created_at")));
					status.setMid(statusJson.getString("mid"));
					if(statusJson.containsKey("original_pic") && statusJson.getString("original_pic") != null && !statusJson.getString("original_pic").equals("")) {
						status.setBmiddle_pic(statusJson.getString("bmiddle_pic"));
						status.setOriginal_pic(statusJson.getString("original_pic"));
						status.setThumbnail_pic(statusJson.getString("thumbnail_pic"));
					}
					com.marketingexpert.service.snsmgnt.User user = new com.marketingexpert.service.snsmgnt.sina.SinaUser();
					JSONObject jsonObjUser = statusJson.getJSONObject("user");
					user.setHead(jsonObjUser.getString("profile_image_url"));
					user.setName(jsonObjUser.getString("screen_name"));
					user.setId(jsonObjUser.getString("id"));
					status.setUser(user);					
					statuses.add(status);					
			}
			return statuses;	
		}

		private static List<Status> generateTencentStatus(
				JSONArray publicweiboArray,String cat,String  q) {
			List<Status> statuses = new ArrayList<Status>();
			JSONObject statusJson = null;
			for(int i=0; i < publicweiboArray.size(); i++) {
				statusJson = publicweiboArray.getJSONObject(i);
				boolean isRetweet = statusJson.getString("type").equals("2");
				if(q == null) {
					Pattern p = Pattern.compile(cat);
					Matcher m = p.matcher(statusJson.getString("text"));
					if(!m.find()) {
						continue;
					}					
					if(isRetweet) {
						statusJson = statusJson.getJSONObject("source");
					}					
			} else {
				if(isRetweet) {
					statusJson = statusJson.getJSONObject("source");
				}
				Pattern p = Pattern.compile(q);
				Matcher m = p.matcher(statusJson.getString("text"));
				if(!m.find()) {
					continue;
				}
			}
				Status status = new Status();
				String sourceUrl = statusJson.containsKey("fromurl")?statusJson.getString("fromurl"):"";
				String sourceName = statusJson.getString("from"); 
				status.setSourceName(sourceName);
				status.setSourceUrl(sourceUrl);
				status.setText(statusJson.getString("text"));
				status.setTimestamp(statusJson.getString("timestamp"));
				status.setDate(new Date(statusJson.getLong("timestamp")* 1000));
				status.setMid(statusJson.getString("id"));
				if(statusJson.containsKey("image") && statusJson.getString("image") != null && !statusJson.getString("image").equals("null")) {
					JSONArray imagejsonArr = statusJson.getJSONArray("image");
					status.setOriginal_pic(imagejsonArr.get(0) + "/460");
					status.setBmiddle_pic(imagejsonArr.get(0) + "/460");
					status.setThumbnail_pic(imagejsonArr.get(0) + "/160");
				}
				com.marketingexpert.service.snsmgnt.User user = new com.marketingexpert.service.snsmgnt.sina.SinaUser();
				user.setHead(statusJson.getString("head"));
				user.setName(statusJson.getString("nick"));
				user.setId(statusJson.getString("name"));
				status.setUser(user);					
				statuses.add(status);		
			}
			return statuses;
		}
	
}
